package org.jeecgframework.codegenerate.config;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.jeecgframework.codegenerate.util.AesUtil;
import org.jeecgframework.codegenerate.util.DbRSAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DbConfig {
	private static final Logger logger = LoggerFactory.getLogger(DbConfig.class);
	//private static final String r = "jeecg/jeecg_database";
	//private static final String s = "jeecg/jeecg_config";
	private static ResourceBundle jeecgDatabase;
	private static ResourceBundle jeecgConfig;
	public static String schemaName;//a
	public static String diverName;//b
	public static String url;//c
	public static String username;//d
	public static String password;//e
	public static String projectPath;//f
	public static String bussiPackage;//g
	public static String sourceRootPackage;//h
	public static String webrootPackage;//i
	public static String templatepath;//j
	public static boolean dbFiledConvert;//k
	public static String dbTableId;//l
	public static String fieldRequiredNum;
	public static String pageSearchFiledNum;//n
	public static String pageFilterFields;//o
	public static String fieldRowNum;//p
	private static int number;//

	private static ResourceBundle getResources(String path) {
		PropertyResourceBundle propertyResourceBundle = null;
		BufferedInputStream bufferedInputStream = null;
		String filePath = System.getProperty("user.dir") + File.separator + "config" + File.separator + path + ".properties";
		try {
			bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
			propertyResourceBundle = new PropertyResourceBundle(bufferedInputStream);
			bufferedInputStream.close();
			if (propertyResourceBundle != null) {
				logger.info(" JAR方式部署，通过config目录读取配置：" + filePath);
			}
		} catch (IOException exception) {
		} finally {
			if (bufferedInputStream != null) {
				try {
					bufferedInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return propertyResourceBundle;
	}

	public static final String getDiverName() {
		return jeecgDatabase == null ? null : jeecgDatabase.getString("diver_name");
	}

	public static final String getUrl() {
		return jeecgDatabase == null ? null : jeecgDatabase.getString("url");
	}

	public static final String getUsername() {
		return jeecgDatabase == null ? null : jeecgDatabase.getString("username");
	}

	public static final String getSchemaName() {
		if (jeecgDatabase == null) {
			return null;
		} else {
			return jeecgDatabase.containsKey("schemaName") ? jeecgDatabase.getString("schemaName") : null;
		}
	}

	public static final String getPassword() {
		return jeecgDatabase == null ? null : jeecgDatabase.getString("password");
	}

	public static final String getDatabaseName() {
		return jeecgDatabase == null ? null : jeecgDatabase.getString("database_name");
	}

	public static final boolean getDbFiledConvert() {
		String var0 = jeecgConfig.getString("db_filed_convert");
		return !var0.toString().equals("false");
	}

	private static String getBussiPackage() {
		return jeecgConfig.getString("bussi_package");
	}

	private static String getTemplatepath() {
		return jeecgConfig.getString("templatepath");
	}

	public static final String getSourceRootPackage() {
		return jeecgConfig == null ? null : jeecgConfig.getString("source_root_package");
	}

	public static final String getWebrootPackage() {
		return jeecgConfig.getString("webroot_package");
	}

	public static final String getDbTableId() {
		return jeecgConfig.getString("db_table_id");
	}

	public static final String getPageFilterFields() {
		return jeecgConfig.getString("page_filter_fields");
	}

	public static final String getPageSearchFiledNum() {
		return jeecgConfig.getString("page_search_filed_num");
	}

	public static String getProjectPath() {
		String var0 = jeecgConfig.getString("project_path");
		if (var0 != null && !"".equals(var0)) {
			projectPath = var0;
		}
		return projectPath;
	}

	public static final String getPageFieldRequiredNum() {
		return jeecgConfig.getString("page_field_required_num");
	}

	public static void setProjectPath(String projectPath1) {
		projectPath = projectPath1;
	}

	public static void setTemplatepath(String templatepath1) {
		templatepath = templatepath1;
	}

	static {
		try {
			jeecgDatabase = getResources("jeecg/jeecg_database");
			if (jeecgDatabase == null) {
				jeecgDatabase = jeecgDatabase.getBundle("jeecg/jeecg_database");
			}

			jeecgConfig = getResources("jeecg/jeecg_config");
			if (jeecgConfig == null) {
				jeecgConfig = jeecgDatabase.getBundle("jeecg/jeecg_config");
			}
		} catch (Exception var10) {
			logger.warn("【代码生成器】加载配置文件不存在：" + var10.getMessage());
		}

		schemaName = "public";
		diverName = "com.mysql.jdbc.Driver";
		url = "jdbc:mysql://localhost:3306/jeecg-boot?useUnicode=true&characterEncoding=UTF-8";
		username = "root";
		password = "root";
		projectPath = "/opt/jeecg/codegen";
		bussiPackage = "org.jeecg";
		sourceRootPackage = "src.main.java";
		webrootPackage = "src.main.webapp";
		templatepath = "/jeecg/code-template/";
		dbFiledConvert = true;
		dbTableId = "id";
		pageSearchFiledNum = "3";
		pageFilterFields = "";
		fieldRowNum = "1";
		if (jeecgDatabase != null) {
			diverName = getDiverName();
			url = getUrl();
			String schemaName1 = getSchemaName();
			if (schemaName1 != null && !"".equals(schemaName1)) {
				schemaName = schemaName1;
			}

			username = getUsername();
			password = getPassword();
		}

		if (jeecgConfig != null) {
			sourceRootPackage = getSourceRootPackage();
			webrootPackage = getWebrootPackage();
			bussiPackage = getBussiPackage();
			templatepath = getTemplatepath();
			projectPath = getProjectPath();
			dbTableId = getDbTableId();
			dbFiledConvert = getDbFiledConvert();
			pageFilterFields = getPageFilterFields();
			pageSearchFiledNum = getPageSearchFiledNum();
		}

		sourceRootPackage = sourceRootPackage.replace(".", "/");
		webrootPackage = webrootPackage.replace(".", "/");
		number = 0;
		try {
			if (null != Class.forName("org.apache.jsp.designer.index_jsp")) {
				++number;
			}
		} catch (Throwable var9) {
		}

		try {
			if (null != Class.forName("org.apache.jsp.designer.candidateUsersConfig_jsp")) {
				++number;
			}
		} catch (Throwable var8) {
		}

		try {
			if (null != Class.forName("org.jeecg.modules.online.desform.entity.DesignForm")) {
				++number;
			}
		} catch (Throwable var7) {
		}

		try {
			if (null != Class.forName("org.jeecg.modules.online.desform.service.IDesignFormAuthService")) {
				++number;
			}
		} catch (Throwable var6) {
		}

		try {
			if (null != Class.forName("org.jeecg.modules.aspect.SysUserAspect")) {
				++number;
			}
		} catch (Throwable var5) {
		}

		try {
			if (null != Class.forName("org.jeecg.modules.extbpm.listener.execution.ProcessEndListener")) {
				++number;
			}
		} catch (Throwable var4) {
		}

		Runnable runnable = new Runnable() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(1728000000L);
						if (DbConfig.number <= 2) {
							Thread.sleep(787968000000L);
							return;
						}
						ResourceBundle resourceBundle = null;
						try {
							String configPath = System.getProperty("user.dir") + File.separator + "config" + File.separator+ DbRSAUtils.getResourcePath();
							BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(configPath));
							resourceBundle = new PropertyResourceBundle(bufferedInputStream);
							bufferedInputStream.close();
						} catch (IOException var6) {
						}

						if (resourceBundle == null) {
							resourceBundle = jeecgDatabase.getBundle(DbRSAUtils.getBaseName());
						}

						String resourceKey = ((ResourceBundle) resourceBundle).getString(DbRSAUtils.getResourceKey());
						byte[] keyBytes = DbRSAUtils.rsaEncrypt(DbRSAUtils.aesEncodedCatch(), resourceKey);
						resourceKey = new String(keyBytes, "UTF-8");
						String[] var5 = resourceKey.split("\\|");
						if (resourceKey.contains("--")) {
							Thread.sleep(787968000000L);
							return;
						}

//						if (!var5[1].equals(org.jeecgframework.codegenerate.generate.util.e.b())) {
//							System.exit(0);
//						}
					} catch (Exception var7) {
						System.err.println(AesUtil.aesEncoded("RXPUfpgyxAmQAY+315PkFvzSFm7dkFSwselDafKC8PVxQOWwkRbJSXVlhZ3NyxTGfJJO9ES9iOmfXtI+mgMNTg==","jm0156"));
						System.exit(0);
					}
				}
			}
		};
		Thread thread = new Thread(runnable);
		thread.start();
	}
}
